package com.rashith.metro.alfa;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ExploreActivity extends Activity implements OnItemClickListener {

	public static final String CATAGORY = "cat";
	private GridView gv;

	private String[] cats = new String[] { "All", "Food", "Shoping",
			"Landmarks", "Fuel", "Bus Stops", "Train", "Airport", "Temple",
			"Church", "Kovil", "Mosque" };
	private int[] icons = new int[] { R.drawable.icon_all,
			R.drawable.icon_food, R.drawable.icon_shopping,
			R.drawable.icon_landmark, R.drawable.icon_fuel,
			R.drawable.icon_transport, R.drawable.icon_train,
			R.drawable.icon_airport, R.drawable.icon_temple,
			R.drawable.icon_church, R.drawable.icon_kovil,
			R.drawable.icon_mosque };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cat_view);

		gv = (GridView) findViewById(R.id.gridView1);

		MyGridAdapter adapter = new MyGridAdapter(this, R.layout.grid_view_row,
				0, cats);
		gv.setAdapter(adapter);
		gv.setOnItemClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.cat_menu, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		
		startActivity(new Intent(ExploreActivity.this, LoginActivity.class));
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.Settings:
			startActivity(new Intent(ExploreActivity.this, AppPref.class));
		}
		return true;
	}

	class MyGridAdapter extends ArrayAdapter<String> {

		public MyGridAdapter(Context context, int resource,
				int textViewResourceId, String[] objects) {
			super(context, resource, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
		}

		private String tag = "Rashith";

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			Log.d(tag, "inside getView " + position);
			LayoutInflater infalter = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View row = infalter.inflate(R.layout.grid_view_row, null);

			TextView tv = (TextView) row.findViewById(R.id.textView1);
			tv.setText(cats[position]);

			ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
			iv.setImageResource(icons[position]);

			return row;
		}

	}

	private boolean netCheck() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnected()) {
			return true;
		}
		Toast.makeText(getApplicationContext(),
				"Internet connectivity is required.", Toast.LENGTH_LONG).show();
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int positon,
			long arg3) {
		// TODO Auto-generated method stub

		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

			Intent intent = new Intent(ExploreActivity.this,
					RashARActivity.class);
			intent.putExtra(CATAGORY, cats[positon]);
			startActivity(intent);
		} else if (netCheck()) {

			Intent i = new Intent(ExploreActivity.this, PlaceListActivity.class);
			i.putExtra(PlaceListActivity.CATEGORY, cats[positon]);
			startActivity(i);
		}

	}
}
